package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.*;
import com.card.hwatt.service.*;
import com.card.hwatt.core.RequestJsonParam;
import com.card.hwatt.dto.*;

/**
 * 维保单位
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/company")
public class CompanyController extends BaseController {

	@Autowired
	@Qualifier("companyService")
	private CompanyService companyService;

	@Autowired
	@Qualifier("departmentService")
	private DepartmentService departmentService;

	@RequestMapping("/index")
	public String index() {
		return "company";
	}

	@RequestMapping("/save")
	public @ResponseBody
	ReturnValue save(@RequestJsonParam Company company) {
		if (company.id == null) {
			company.id = UUID.randomUUID();
			companyService.save(CompanyAssembler.convert(company));
		} else {
			companyService.update(CompanyAssembler.convert(company));
		}

		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	PageResult<Company> loadAll() {
		List<Object> params = new ArrayList<Object>();
		params.add(2L);

		PageResult<Company> result = CompanyAssembler.convert(departmentService
				.loadAll(new PageParam(0, 0, " parentId = ? ", params,
						" orderBy asc ")));

		return result;
	}
}
